export default class Tools {
  // 判断是否是浏览器环境
  public inBrowser = typeof window !== 'undefined';
  // 获取浏览器ua
  public UA = this.inBrowser && window.navigator.userAgent.toLowerCase();
  // 当前浏览器是否是ie
  public isIE = this.UA && /msie|trident/.test(this.UA);
  //当前浏览器是ie9
  public isIE9 = this.UA && this.UA.indexOf('msie 9.0') > 0;
  // 当前浏览器是edge
  public isEdge = this.UA && this.UA.indexOf('edge/') > 0;
  //当前浏览器是android
  public isAndroid = (this.UA && this.UA.indexOf('android') > 0);
  // 当前浏览器是ios
  public isIOS = (this.UA && /iphone|ipad|ipod|ios/.test(this.UA));
  // 当前浏览器是chrome
  public isChrome = this.UA && /chrome\/\d+/.test(this.UA) && !this.isEdge;
  // 当前环境是phantomjs
  public isPhantomJS = this.UA && /phantomjs/.test(this.UA);
  // 当前浏览器是火狐
  public isFF = this.UA && this.UA.match(/firefox\/(\d+)/);
  // 对象是否存在__proto__
  public hasProto = '__proto__' in {};
  // 判断变量是否是undefined
  public isUndef(v: any) {
    return v === undefined || v === null
  }
  //判断变量是否已经定义
  public isDef(v: any) {
    return v !== undefined && v !== null
  }
  // 判断变量是否为真
  public isTrue(v: any) {
    return v === true
  }
  // 判断变量是否为假
  public isFalse(v: any) {
    return v === false
  }
  // 判断变量是否是对象
  public isObject(obj: any) {
    return obj !== null && typeof obj === 'object'
  }
  // 判断变量是否是正则
  public isRegExp(v: any) {
    return Object.prototype.toString.call(v) === '[object RegExp]'
  }
  // 判断变量是否是Promise
  public isPromise(val: any) {
    return (
      this.isDef(val) &&
      typeof val.then === 'function' &&
      typeof val.catch === 'function'
    )
  }
  // 转化number
  public toNumber(val: any, type: string) {
    if (this.isUndef(type) || type === "float" || type === "double") {
      let n = parseFloat(val);
      return isNaN(n) ? val : n;
    } else {
      let n = parseInt(val);
      return isNaN(n) ? val : n;
    }
  }
}